'use strict';
angular.module('sbAdminApp')
    .controller('SettlementSchedulerCenterCtrl', ['$scope', '$modal', '$log', '$location', 'settlementSchedulerCenterService', function ($scope, $modal, $log, $location, settlementSchedulerCenterService) {
        //初始化作用域变量
        $scope.lists = [];
        $scope.totalItems = 0;
        $scope.currentPage = 1;
        $scope.itempersize = 10;
   
        //切换分页事件时触发方法
        $scope.pageChanged = function () {
        	 $scope.search1();
        };
        
        //点击查询触发
        $scope.search1 = function () {
        	var begin = $("#beginDate").val(); 
        	var end = $("#endDate").val();
        	settlementSchedulerCenterService.search1($scope.taskName,
        			begin,end, 
            		$scope.currentPage, 
            		$scope.itempersize).then(function (dataResponse) {
            	$scope.lists = dataResponse.data['result'];
                $scope.totalItems = dataResponse.data['page'].totalRecord;
            });
        };
        
        
        //刷新
        $scope.refresh1 = function () {
        	settlementSchedulerCenterService.search1(null, null, null, 
            		$scope.currentPage, 
            		$scope.itempersize).then(function (dataResponse) {
            	$scope.lists = dataResponse.data['result'];
                $scope.totalItems = dataResponse.data['page'].totalRecord;
            });
        };
        
        
          //删除
        $scope.delete1 = function (id) {
        	settlementSchedulerCenterService.delete1(id).then(function (dataResponse) {
                if(dataResponse.data['result']>0){
                	alert("删除成功！");
                	$scope.search1();
                	//重新加载数据
                }else{
                	alert("删除失败！");
                }
            });
        };
        
        
        
        $scope.edit1 = function (ssc) {
            var modalInstance = $modal.open({
                templateUrl: 'views/service/scheduler/edit.html',
                controller: 'schedulerEditCtrl',
                resolve: {
                	'ssc': function () {  return angular.copy(ssc);   }
                }
            });
            modalInstance.result.then(function () {
                $scope.search1();
            }, function () {
            });
        };
        
        $scope.add1 = function () {
            var modalInstance = $modal.open({
                templateUrl: 'views/service/scheduler/add.html',
                controller: 'schedulerAddCtrl',
                resolve: {
                	 
                }
                
            });
            modalInstance.result.then(function () {
                $scope.search1();
            }, function () {
            });
        };
        //加载作用域数据
        $scope.search1();;

    }]);



angular.module('sbAdminApp').controller('schedulerAddCtrl', function ($scope, $modalInstance, settlementSchedulerCenterService, $log, $location ) {
	//添加
    $scope.add = function () {
    	var taskName=$scope.taskName;
    	var tickTime=$scope.tickTime;
    	var currentMachine=$scope.currentMachine;
    	var threshold=$scope.threshold;
    	if(  (taskName==null || taskName=="" ||taskName==undefined )
    			&& (currentMachine==null || currentMachine=="" || currentMachine==undefined )
    			&& (threshold==null || threshold=="" || threshold==undefined )
    			&& (tickTime==null || tickTime=="" || tickTime==undefined ) ){
    		    $modalInstance.close();
    	}else{
    	 	settlementSchedulerCenterService.add1( taskName,currentMachine,threshold,tickTime).then(function (dataResponse) {
    			//添加成功
    	     if(dataResponse.data['result']==1){
    	          $modalInstance.close();
    	     }else{
    	          	alert("添加失败");
    	     }
           });
    	}
    	
    	
   
    };

    //关闭模态框
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
 
});



angular.module('sbAdminApp').controller('schedulerEditCtrl', function ($scope,ssc, $modalInstance, settlementSchedulerCenterService, $log, $location ) {
	$scope.ssc=ssc; 
	//修改 
    $scope.edits = function () {
    	var tickTime=$scope.tickTime;
    	var currentMachine=$scope.currentMachine;
    	var threshold=$scope.threshold;
    	if( (currentMachine==null || currentMachine=="" || currentMachine==undefined )
    			&& (threshold==null || threshold=="" || threshold==undefined )
    			&& (tickTime==null || tickTime=="" || tickTime==undefined ) ){
    		    $modalInstance.close();
    	}else{
    		settlementSchedulerCenterService.edit1(ssc.id,tickTime,currentMachine,threshold ).then(function (dataResponse) {
    			//添加成功
	    	   if(dataResponse.data['result']==1){
	    	        $modalInstance.close();
	    	   }else{
	    	        	alert("修改失败");
	    	   }
            });
    	}
    	
    };

    //关闭模态框
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
 
});










